//
//  SLRedDotLabel.m
//  SLView
//
//  Created by Victor on 15/12/4.
//  Copyright © 2015年 Sunline. All rights reserved.
//

#import "JFRedDotLabel.h"
#import "JFKitPri.h"

static NSInteger kDefaultMaxCount = 99;

@interface JFRedDotLabel ()


@end

@implementation JFRedDotLabel

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}



- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = CGRectGetHeight(self.bounds) / 2;
    self.layer.masksToBounds = YES;
    
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    self.backgroundColor = [UIColor redColor];
}


- (void)drawTextInRect:(CGRect)rect {
    if (self.showDot) {
        rect.size.width = self.dotSize.width;
        rect.size.height = self.dotSize.height;
    }
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    if (![self.text length] && ![self.attributedText length]) {
        return CGRectZero;
    }
    UIEdgeInsets insets = self.insets;
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
                    limitedToNumberOfLines:numberOfLines];
    
    rect.origin.x    -= insets.left;
    rect.origin.y    -= insets.top;
    rect.size.width  += (insets.left + insets.right);
    rect.size.height += (insets.top + insets.bottom);

    if (self.text.length == 1) {
        CGFloat max =  MAX(rect.size.width, rect.size.height);
        rect.size.width = max;
        rect.size.height = max;
    }else if (rect.size.width < rect.size.height) {
        rect.size.width = rect.size.height;
    }
    if (self.showDot) {
        rect.size.width = self.dotSize.width;
        rect.size.height = self.dotSize.height;
    }
    return rect;
}
#pragma mark - Private Methods
- (void)setup {
    self.font = [UIFont systemFontOfSize:12.f];
    self.dotSize = CGSizeMake(10, 10);
    self.maxCount = kDefaultMaxCount;
    self.insets = UIEdgeInsetsMake(0, 5, 0, 5);
    self.textAlignment = NSTextAlignmentCenter;
    self.backgroundColor = [UIColor redColor];
    self.textColor = [UIColor colorWithHexString:@"#ffffff"];
}

- (void)updateCount {
    if (!self.count) {
        self.text = @"";
    }else {
        if (self.showDot) {
            self.text = @" ";
        }else if (self.maxCount && self.count > self.maxCount) {
            self.text = [NSString stringWithFormat:@"%@+",@(self.maxCount)];
        }else {
            self.text = [@(self.count) stringValue];
        }
    }
    if (self.showDot) {
        self.insets = UIEdgeInsetsZero;
    }
    [self layoutIfNeeded];
    
    //victor lau add
    [self sizeToFit];
    
}

#pragma mark - Setter / Getter
- (void)setCount:(NSInteger)count {
    _count = count;
    [self updateCount];
}

- (void)setMaxCount:(NSInteger)maxCount {
    _maxCount = maxCount;
    [self updateCount];
}

- (void)setShowDot:(BOOL)showDot {
    _showDot = showDot;
    [self updateCount];
}

- (void)setDotSize:(CGSize)dotSize {
    _dotSize = dotSize;
    [self updateCount];
}

@end
